package cn.hx.mall.goods.controller;

import cn.hx.mall.goods.entity.Brand;
import cn.hx.mall.goods.entity.Product;
import cn.hx.mall.goods.entity.ProductCategory;
import cn.hx.mall.goods.entity.SkuStock;
import cn.hx.mall.goods.service.BrandService;
import cn.hx.mall.goods.service.ProductCategoryService;
import cn.hx.mall.goods.service.ProductService;
import cn.hx.mall.goods.service.SkuStockService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * 商品信息(Product)表控制层
 *
 * @author makejava
 * @since 2022-11-02 15:56:45
 */
@Controller
@RequestMapping("goods")
public class ProductController {
    /**
     * 服务对象
     */
    @Resource
    private ProductService productService;
    @Resource
    private BrandService brandService;
    @Resource
    private ProductCategoryService productCategoryService;
    @Resource
    private SkuStockService skuStockService;



    private void load1(Model model){
        List<Brand> brands = brandService.queryAllByLimit(0, 1000);
        List<ProductCategory> productCategories = productCategoryService.queryAllByLimit(0, 1000);
        model.addAttribute("category",productCategories);
        model.addAttribute("brands",brands);
    }

    private void pageHelp(List<Product> products, Model model){
        PageInfo<Product> pageInfo = new PageInfo<>(products);
        model.addAttribute("pageInfo",pageInfo);
    }

    private void load(Model model){
        List<Brand> brands = brandService.queryAllByLimit(0, 1000);
        List<ProductCategory> productCategories = productCategoryService.queryAllByLimit(0, 1000);
        model.addAttribute("category",productCategories);
        model.addAttribute("brands",brands);
    }
    @GetMapping("productList")
    public String List(Model model, PageInfo pageInfo,Product product){
        if (pageInfo.getPageNum()==0){
            pageInfo.setPageNum(1);
        }if(pageInfo.getPageSize()==0){
            pageInfo.setPageSize(5);
        }
        PageHelper.startPage(pageInfo);
        List<Product> products = productService.queryAll(product);
        PageInfo productPageInfo = new PageInfo<>(products);
        model.addAttribute("pageInfo", productPageInfo);
        load(model);
        return "goods/productList";
    }
    @PostMapping("findProductList")
    public String findProductList(Product product, Model model, PageInfo pageInfo, HttpSession session){
        if (pageInfo.getPageNum()==0){
            pageInfo.setPageNum(1);
        }if(pageInfo.getPageSize()==0){
            pageInfo.setPageSize(5);
        }
        PageHelper.startPage(pageInfo);
        List<Product> products = productService.queryAll(product);
        PageInfo pageInfo1 = new PageInfo<>(products);
//        pageHelp(products,model);
        load(model);
        model.addAttribute("pageInfo",pageInfo1);
//        分页条件
        session.setAttribute("product",product);
        return "goods/productList";
    }
    @GetMapping("findProductList")
    public String resource(Model model, PageInfo pageInfo,HttpSession session) {
        if (pageInfo.getPageNum()==0){
            pageInfo.setPageNum(1);
        }if(pageInfo.getPageSize()==0){
            pageInfo.setPageSize(5);
        }
        PageHelper.startPage(pageInfo);
        Product pagePro = (Product) session.getAttribute("product");
        List<Product> products = productService.queryAll(pagePro);
        PageInfo productPageInfo = new PageInfo<>(products);
        model.addAttribute("pageInfo", productPageInfo);
        load(model);
        return "goods/productList";
    }

    @GetMapping("setNull")
    public String setNull(Model model, Integer pageNum,HttpSession session){
        if (pageNum==null){
            pageNum=1;
        }
        Page<Object> objects = PageHelper.startPage(pageNum, 5);
        List<Product> products = productService.queryAll(new Product());
        pageHelp(products,model);
        session.setAttribute("product",null);
        Product product = new Product();
        load(model);
        model.addAttribute("products",products);
        model.addAttribute("product",product);
        return "goods/productList";
    }


    @GetMapping("deleteProduct")
    public String deleteProduct(Long id, Model model){
        productService.deleteById(id);
        return "redirect:productList";
    }
    @GetMapping("sw")
    @ResponseBody
    public Integer switchStatus(Long id,Product product1){
        Product product = productService.queryById(id);
        if (product1.getPublishStatus()!=null) {
            if (product.getPublishStatus() == 1) {
                product.setPublishStatus(0);
                Product update = productService.update(product);
                return 0;
            } else {
                product.setPublishStatus(1);
                Product update = productService.update(product);
                return 1;
            }
        }
        if (product1.getNewStatus()!=null) {
            if (product.getNewStatus() == 1) {
                product.setNewStatus(0);
                Product update = productService.update(product);
                return 0;
            } else {
                product.setNewStatus(1);
                Product update = productService.update(product);
                return 1;
            }
        }
        if (product1.getRecommandStatus()!=null) {
            if (product.getRecommandStatus() == 1) {
                product.setRecommandStatus(0);
                Product update = productService.update(product);
                return 0;
            } else {
                product.setRecommandStatus(1);
                Product update = productService.update(product);
                return 1;
            }
        }
        return null;
    }
    @GetMapping("alterProduct")
    @ResponseBody
    public List<SkuStock> alter(Long productId){
        SkuStock skuStock = new SkuStock();
        skuStock.setProductId(productId);
        List<SkuStock> skuStocks = skuStockService.queryAll(skuStock);
        return skuStocks;
    }

    @GetMapping("/detail")
    public String ProductDetail(Long id, Model model){
        load1(model);
        model.addAttribute("pro",productService.queryById(id));
        return "goods/productDetail";
    }


}